﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Classifieds
{
    public static class Create
    {
        public static Adams.Admin.DataTransfer.Classifieds.Create.PageLoad PageLoad(int id)
        {
            var data = new Adams.Admin.DataTransfer.Classifieds.Create.PageLoad();
            data.Posting = new Shared.Entities.Classifieds.Posting();
            data.Types = new Shared.Entities.Classifieds.TypeCollection();
            data.Categories = new Shared.Entities.Classifieds.CategoryCollection();

            using (SqlDataReader dr = DB.RunDataReader(@"
IF(@PostingID > 0)
BEGIN
    DECLARE @TypeID int
    DECLARE @CategoryID int
    SELECT @CategoryID = CategoryID FROM Classifieds.Postings WHERE PostingID = @PostingID
    SELECT @TypeID = TypeID FROM Classifieds.Categories WHERE CategoryID = @CategoryID

    SELECT P.PostingID,P.CategoryID,P.MemberID,P.StatusID,P.Title,P.Price,P.Location,P.ReplyTo,P.Description,P.CreatedOn,P.ExpiresOn,M.FirstName,M.LastName,M.Email,T.TypeID
    FROM Classifieds.Postings P INNER JOIN Classifieds.Categories C ON P.CategoryID = C.CategoryID INNER JOIN Classifieds.Types T ON C.TypeID = T.TypeID INNER JOIN Membership.Members M ON P.MemberID = M.MemberID
    WHERE PostingID = @PostingID

    SELECT * FROM Classifieds.Categories WHERE TypeID = @TypeID ORDER BY Name
END
SELECT * FROM Classifieds.Types ORDER BY Name
", CommandType.Text,
            DB.CreateParameter("@PostingID", id, SqlDbType.Int)))
            {
                if (id > 0)
                {
                    dr.Read();
                    data.Posting.ID = dr["PostingID"].DbInt();
                    data.Posting.Status = (Shared.Entities.Classifieds.Status)dr["StatusID"].DbInt();
                    data.Posting.Title = dr["Title"].DbString();
                    data.Posting.Price = dr["Price"].DbString();
                    data.Posting.Location = dr["Location"].DbString();
                    data.Posting.ReplyTo = dr["Location"].DbString();
                    data.Posting.Description = dr["Description"].DbString();
                    data.Posting.CreatedOn = dr["CreatedOn"].DbDate();
                    data.Posting.ExpiresOn = dr["ExpiresOn"].DbDate();
                    data.Posting.Category = new Shared.Entities.Classifieds.Category()
                    {
                        ID = dr["CategoryID"].DbInt(),
                        Type = new Shared.Entities.Classifieds.Type()
                        {
                            ID = dr["TypeID"].DbInt()
                        }
                    };
                    data.Posting.Member = new Shared.Entities.Membership.Member()
                    {
                        ID = dr["MemberID"].DbInt(),
                        Email = dr["Email"].DbString(),
                        FirstName = dr["FirstName"].DbString(),
                        LastName = dr["LastName"].DbString(),
                    };

                    dr.NextResult();
                    while (dr.Read())
                    {
                        var cat = new Shared.Entities.Classifieds.Category();
                        cat.ID = dr["CategoryID"].DbInt();
                        cat.Name = dr["Name"].DbString();
                        data.Categories.Add(cat);
                    }
                    dr.NextResult();
                }
                while (dr.Read())
                {
                    var typ = new Shared.Entities.Classifieds.Type();
                    typ.ID = dr["TypeID"].DbInt();
                    typ.Name = dr["Name"].DbString();
                    data.Types.Add(typ);
                }
            }

            return data;
        }

        public static Adams.Shared.Entities.Classifieds.CategoryCollection GetCategoriesByType(int typeId)
        {
            var data = new Shared.Entities.Classifieds.CategoryCollection();

            if (typeId > 0)
            {
                using (SqlDataReader dr = DB.RunDataReader(@"
SELECT * FROM Classifieds.Categories WHERE TypeID = @TypeID ORDER BY Name
", CommandType.Text,
                DB.CreateParameter("@TypeID", typeId, SqlDbType.Int)))
                {
                    while (dr.Read())
                    {
                        var cat = new Shared.Entities.Classifieds.Category();
                        cat.ID = dr["CategoryID"].DbInt();
                        cat.Name = dr["Name"].DbString();
                        data.Add(cat);
                    }
                }
            }

            return data;
        }

        public static void Save(DataTransfer.Classifieds.Create.FormInput input)
        {
            DB.Run(@"
IF(@PostingID > 0)
BEGIN
    UPDATE Classifieds.Postings SET
        CategoryID = @CategoryID
        ,MemberID = @MemberID
        ,StatusID = @StatusID
        ,Title = @Title
        ,Price = @Price
        ,Location = @Location
        ,ReplyTo = @ReplyTo
        ,Description = @Description
        ,ExpiresOn = @ExpiresOn
    WHERE PostingID = @PostingID
END
ELSE
BEGIN
    INSERT INTO Classifieds.Postings (
        CategoryID
        ,MemberID
        ,StatusID
        ,Title
        ,Price
        ,Location
        ,ReplyTo
        ,Description
        ,ExpiresOn)
    VALUES (
        @CategoryID
        ,@MemberID
        ,@StatusID
        ,@Title
        ,@Price
        ,@Location
        ,@ReplyTo
        ,@Description
        ,@ExpiresOn)
END
    ", CommandType.Text,
                    DB.CreateParameter("@PostingID", input.ID.ToInt()),
                    DB.CreateParameter("@CategoryID", input.CategoryID.ToInt()),
                    DB.CreateParameter("@MemberID", input.MemberID.ToInt()),
                    DB.CreateParameter("@StatusID", input.StatusID.ToInt()),
                    DB.CreateParameter("@Title", input.Title.ToNString()),
                    DB.CreateParameter("@Price", input.Price.ToNString()),
                    DB.CreateParameter("@Location", input.Location.ToNString()),
                    DB.CreateParameter("@ReplyTo", input.ReplyTo.ToNString()),
                    DB.CreateParameter("@Description", input.Description.ToNString()),
                    DB.CreateParameter("@ExpiresOn", input.ExpiresOn.ToDateTime()));
        }
    }
}
